package com.woniuxy.service.impl;

import com.woniuxy.dao.RbacMenuDao;
import com.woniuxy.entity.MenuEntity;
import com.woniuxy.entity.MenusRoleDTO;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.RbacMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 马宇航
 * @Do: 菜单实现类业务层
 * @DateTime: 22/05/25/0025 上午 10:44
 * @Component: 成都蜗牛学苑
 **/
@Service
public class RbacMenuServiceImpl implements RbacMenuService {
    @Autowired(required = false)
    RbacMenuDao rbacMenuDao;
    @Override
    public ResponseEntity findAll() {
        List<MenuEntity> allMenu = rbacMenuDao.findAll();
        return new ResponseEntity(allMenu);
    }

    @Override
    public ResponseEntity getFatherMenu() {
        List<MenuEntity> fatherMenu = rbacMenuDao.getFatherMenu();
        return new ResponseEntity(fatherMenu);
    }

    @Override
    public ResponseEntity saveMenu(MenuEntity menuEntity) {
        int i = rbacMenuDao.saveMenu(menuEntity);
        if (i>0){
            return ResponseEntity.SUCCESS;
        }else {
            return ResponseEntity.ERROR;
        }
    }

    @Override
    public ResponseEntity findAllByRoleId(int roleId) {
        List<Integer> allMenu = rbacMenuDao.findAllByRoleId(roleId);
        return new ResponseEntity(allMenu);
    }

    @Override
    @Transactional
    public ResponseEntity saveMenusByRoleId(MenusRoleDTO menusRoleDTO) {
        //我只需要操作中间表 rbac_role_menu表即可
        // 1. 先通过角色id 删除它所有的菜单权限信息
        int i = rbacMenuDao.deleteByRoleId(menusRoleDTO.getRoleId());
        // 2. 批量插入该角色和所有id信息 <foreach>
        int j = rbacMenuDao.saveMenusByRoleId(menusRoleDTO);
        // 3. 统一事务管理
        if (j>0){
            return ResponseEntity.SUCCESS;
        }else {
            return ResponseEntity.ERROR;
        }
    }

    @Override
    public ResponseEntity getMenusByRoleId(int roleId) {
        // 1.首先，查询所有的一级菜单
        List<MenuEntity> firstMenus = rbacMenuDao.getFirstMenus();
        // 2.查出该角色所拥有的所有的下级菜单  子菜单已经递归完成
        List<MenuEntity> allSonMenu = rbacMenuDao.getMenusByRoleId(roleId);
        // 3.通过代码的手段，把二级菜单使用递归的方式放入一级菜单中
        // 遍历所有一级菜单的内容
        for (MenuEntity firstMenu : firstMenus) {
            List<MenuEntity> menuEntities = new ArrayList<>();
            //遍历子菜单信息，拿到每一行数据
            for (MenuEntity sonMenu : allSonMenu) {
                //子菜单的父id，等于一级菜单的id的时候
                if (sonMenu.getFid() == firstMenu.getId()){
                    //把这个子菜单放入 父级菜单的children属性中
                    menuEntities.add(sonMenu);
                }
            }
            //每次封装完成子菜单数据以后，放入一级菜单中
            firstMenu.setChildren(menuEntities);
        }
        System.out.println(firstMenus);
        return new ResponseEntity(firstMenus);
    }
    /**
     * 提供一个递归方法，来遍历添加多级子菜单
     * ChangeLog : 1. 创建 (22/05/25/0025 下午 5:39 [马宇航]);
     *
     * @return java.util.List<com.woniuxy.entity.MenuEntity>
    */
//    public List<MenuEntity> recursiveMultilevelMenu(List<MenuEntity> fatherMenu){
//        //遍历子菜单信息，拿到每一行数据
//        for (MenuEntity sonMenu : fatherMenu) {
//            //子菜单的父id，等于一级菜单的id的时候
//            if (sonMenu.getFid() == firstMenu.getId()){
//                //把这个子菜单放入 父级菜单的children属性中
//                menuEntities.add(sonMenu);
//            }
//            //递归逻辑 sonMenu 就是 allSonMenu
//        }
//
//        return recursiveMultilevelMenu(fatherMenu);
//    }
}
